﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Golf.WebUI.AdminShow.CommoditiesMmanagement
{
    public partial class Golf_ProductModify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindTypeName();
                BindPinPai();
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int Id = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(Id);
                }
            }
        }
        /// <summary>
        /// 绑定产品类型数据
        /// </summary>
        public void BindTypeName()
        {
            Golf.BLL.Golf_ProductType bll = new Golf.BLL.Golf_ProductType();
            DropDownList1.DataSource = bll.GetList("IsParent='false'");//绑定不父类的产品类型
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem() { Value = "0", Text = "--请选择--" });
        }

        /// <summary>
        /// 绑定产品 品牌
        /// </summary>
        public void BindPinPai()
        {
            Golf.BLL.Golf_BrandType bll = new Golf.BLL.Golf_BrandType();
            DropDownList2.DataSource = bll.GetList("IsDisplay=1");//绑定 数据库中显示的产品品牌
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, new ListItem() { Value = "0", Text = "--请选择--" });
        }
        private void ShowInfo(int Id)
        {
            Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
            Golf.Model.Golf_Product model = bll.GetModel(Id);
            this.lblId.Text = model.Id.ToString();
            try
            {
                DropDownList1.Items.FindByValue(model.Pro_TypeId.ToString()).Selected = true;
                DropDownList2.Items.FindByValue(model.Pro_BrandTypeId.ToString()).Selected = true;
            }
            catch (Exception)
            {

            }
            this.txtPro_Name.Text = model.Pro_Name;
            this.txtPro_Number.Text = model.Pro_Number;
            this.txtPro_BuyNum.Text = model.Pro_BuyNum.ToString();
            this.txtPro_SeoTitle.Text = model.Pro_SeoTitle;

            this.txtPro_Wood.Text = model.Pro_Wood;
            this.txtPro_MarketPrice.Text = model.Pro_MarketPrice.ToString();
            this.txtPro_Price.Text = model.Pro_Price.ToString();
            this.chkIsShow.Checked = model.IsShow;
            this.chkisCommend.Checked = model.isCommend;
            this.txtLimitBuyNum.Text = model.LimitBuyNum.ToString();
            this.txtInventoryNum.Text = model.InventoryNum.ToString();
            this.chkIsStock.Checked = model.IsStock;
            this.txtPro_CommentNum.Text = model.Pro_CommentNum.ToString();
            this.chkIsGrounding.Checked = model.IsGrounding;
            this.txtPro_Star.Text = model.Pro_Star.ToString(); ;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (!PageValidate.IsNumber(DropDownList1.SelectedValue))
            {
                strErr += "产品分类id	Golf_Pro格式错误！\\n";
            }
            if (!PageValidate.IsNumber(DropDownList2.SelectedValue))
            {
                strErr += "产品品牌分类id	Golf_B格式错误！\\n";
            }
            if (this.txtPro_Name.Text.Trim().Length == 0)
            {
                strErr += "产品名称不能为空！\\n";
            }
            if (this.txtPro_Number.Text.Trim().Length == 0)
            {
                strErr += "商品编号不能为空！\\n";
            }
            if (!PageValidate.IsNumber(txtPro_BuyNum.Text))
            {
                strErr += "购买数量格式错误！\\n";
            }
            //if (this.txtPro_Picture.Text.Trim().Length == 0)
            //{
            //    strErr += "产品默认图 160-160不能为空！\\n";
            //}
            //if (this.txtPro_PicSmall.Text.Trim().Length == 0)
            //{
            //    strErr += "300-300不能为空！\\n";
            //}
            //if (this.txtPro_PicBig.Text.Trim().Length == 0)
            //{
            //    strErr += "800-800不能为空！\\n";
            //}
            if (this.txtPro_SeoTitle.Text.Trim().Length == 0)
            {
                strErr += "seo标题不能为空！\\n";
            }

            if (this.txtPro_Wood.Text.Trim().Length == 0)
            {
                strErr += "产品号数不能为空！\\n";
            }

            if (!PageValidate.IsDecimal(txtPro_MarketPrice.Text))
            {
                strErr += "市场价格式错误！\\n";
            }
            if (!PageValidate.IsDecimal(txtPro_Price.Text))
            {
                strErr += "会员价格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtLimitBuyNum.Text))
            {
                strErr += "限制购买时间格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtInventoryNum.Text))
            {
                strErr += "库存时间格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtPro_CommentNum.Text))
            {
                strErr += "产品评论数格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtPro_Star.Text))
            {
                strErr += "1-100分 平分错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int Id = int.Parse(this.lblId.Text);
            int Pro_TypeId = int.Parse(DropDownList1.SelectedValue);
            int Pro_BrandTypeId = int.Parse(DropDownList2.SelectedValue);
            string Pro_Name = this.txtPro_Name.Text;
            string Pro_Number = this.txtPro_Number.Text;
            int Pro_BuyNum = int.Parse(this.txtPro_BuyNum.Text);
            string Pro_SeoTitle = this.txtPro_SeoTitle.Text;

            string Pro_Wood = this.txtPro_Wood.Text;
            decimal Pro_MarketPrice = decimal.Parse(this.txtPro_MarketPrice.Text);
            decimal Pro_Price = decimal.Parse(this.txtPro_Price.Text);
            bool IsShow = this.chkIsShow.Checked;
            bool isCommend = this.chkisCommend.Checked;
            int LimitBuyNum = int.Parse(this.txtLimitBuyNum.Text);
            int InventoryNum = int.Parse(this.txtInventoryNum.Text);
            bool IsStock = this.chkIsStock.Checked;
            int Pro_CommentNum = int.Parse(this.txtPro_CommentNum.Text);
            bool IsGrounding = this.chkIsGrounding.Checked;
            int Pro_Star = int.Parse(txtPro_Star.Text);


            Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
            Golf.Model.Golf_Product model = bll.GetModel(Id);
            if (model == null) return;
            model.Id = Id;
            model.Pro_TypeId = Pro_TypeId;
            model.Pro_BrandTypeId = Pro_BrandTypeId;
            model.Pro_Name = Pro_Name;
            model.Pro_Number = Pro_Number;
            model.Pro_BuyNum = Pro_BuyNum;
            model.Pro_SeoTitle = Pro_SeoTitle;

            model.Pro_Wood = Pro_Wood;

            model.Pro_Save = 0;
            model.Pro_MarketPrice = Pro_MarketPrice;
            model.Pro_Price = Pro_Price;
            model.IsShow = IsShow;
            model.isCommend = isCommend;
            model.LimitBuyNum = LimitBuyNum;
            model.InventoryNum = InventoryNum;
            model.IsStock = IsStock;
            model.Pro_CommentNum = Pro_CommentNum;
            model.IsGrounding = IsGrounding;
            model.Pro_Star = Pro_Star;

            if (bll.Update(model))
            {
                Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('添加成功！');", true);
            }
            else
            {

                Page.ClientScript.RegisterStartupScript(typeof(Page), "", "ymPrompt.errorInfo('保存失败');", true);
            }

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('请点击确定取消当前操作！');", true);
        }
    }
}